package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.MyPage;
import com.itheima.bean.Orders;
import com.itheima.result.R;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @PostMapping("/submit")
    public R add(@RequestBody Orders orders){
        //调用service层的方法进行添加菜品到购物车
        int row = orderService.add(orders);
        //判断返回得影响行数，是否添加成功，返回相应的数据
        if (row > 0){
            //进入这里表示订单已经支付成功，清除购物车数据
            orderService.cleanShoppingCart();
            return R.success("添加成功！");
        }else{
            return R.error("添加失败！");
        }
    }

    @GetMapping("/userPage")
    public R page(MyPage page){
        //调用service 层的方法进行分页查询
        IPage<Orders> p = orderService.page(page);
        //返回查询到的IPage对象
        return R.success(p);
    }
}
